package com.skyline.base.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.skyline.base.dao.CommentDao;
import com.skyline.base.mapper.CommentMapper;
import com.skyline.base.model.Comment;
import com.skyline.common.bean.Page;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.GenericTable;
import com.skyline.common.cache.annotation.Param;

/**
 * 评论模块的Dao
 * 
 * @author Jairus Chan
 * @version 0.1
 * */
@Repository("commentDao")
public class CommentDaoImpl extends BaseDaoImpl implements CommentDao {
	/**
	 * insert into {0}
	 * (userid,nickname,portrait,content,createtime,ref,resource,
	 * refed,agree,disagree) values(?,?,?,?,?,?,?,?,0,0)
	 * */
	@Value("${CommentDao.insertComment}")
	private String insertCommentSql;

	/**
	 * select * from {0} where resource=? order by commentid desc
	 * */
	@Value("${CommentDao.queryCommentByResource}")
	private String queryCommentByResourceSql;

	/**
	 * select * from {0} where resource=? and refed=1 order by ref desc
	 * */
	@Value("${CommentDao.queryRefedCommentByResource}")
	private String queryRefedCommentByResourceSql;

	/**
	 * update {0} set refed=1 where commentid=?
	 * */
	@Value("${CommentDao.updateCommentRef}")
	private String updateCommentRefSql;

	/**
	 * update {0} set agree=agree+1 where commentid=?
	 * */
	@Value("${CommentDao.updateCommentAgree}")
	private String updateCommentAgreeSql;

	/**
	 * update {0} set disagree=disagree+1 where commentid=?
	 * */
	@Value("${CommentDao.updateCommentDisagree}")
	private String updateCommentDisagreeSql;

	/**
	 * delete from {0} where commentid=? and (userid=? or resourceowner=?)
	 * */
	@Value("${CommentDao.deleteComment}")
	private String deleteCommentSql;

	/**
	 * select * from {0} where commentid=? limit 1;
	 * */
	@Value("${CommentDao.queryCommentById}")
	private String queryCommentByIdSql;

	@CacheDelete(type = CacheCategoryType.COMMENT)
	public void insertComment(Long commenterId, String commenterNickname, String commenterPortrait, String content, Long ref,
			@Fk Object resourceId, @GenericTable String commentType, long ownerId) {
		String sql = genSql(insertCommentSql, commentType);
		jdbcTemplate.update(sql, commenterId, commenterNickname, commenterPortrait, content, ref, resourceId, 0, ownerId);
	}

	@Deprecated
	public List<Comment> findAll(String commentType) {
		return null;
	}

	@Cache(keyPattern = "comment-l-r:rid", type = CacheCategoryType.COMMENT, pagination = true)
	public List<Comment> queryCommentByResource(@Param("rid") @Fk Object resourceId, Page page, @GenericTable String commentType) {
		String sql = genSql(queryCommentByResourceSql, commentType);
		//System.out.println(commentType);
		List<Comment> comments = getPaginationResult(sql, page, CommentMapper.getMapper(commentType), resourceId);
		return comments;
	}

	@Cache(keyPattern = "comment-l-rf-r:rid", type = CacheCategoryType.COMMENT)
	public List<Comment> queryRefedCommentByResource(@Param("rid") @Fk Object resourceId, @GenericTable String commentType) {
		String sql = genSql(queryRefedCommentByResourceSql, commentType);
		return jdbcTemplate.query(sql, CommentMapper.getMapper(commentType), resourceId);
	}

	public List<Comment> queryCommentByCommenter(Long commenterId, String commentType) {
		// TODO:还没有实现
		return null;
	}

	// @CacheDelete(type=CacheCategoryType.COMMENT)
	public void updateCommentRef(@Fk Object resourceId, Long commentId, @GenericTable String commentType) {
		String sql = genSql(updateCommentRefSql, commentType);
		jdbcTemplate.update(sql, commentId);
	}

	public void updateCommentContent(Object resourceId, Long commentId, String content, String resourceComment) {
	}

	@CacheDelete(type = CacheCategoryType.COMMENT)
	public void updateCommentAgree(@Fk Object resourceId, Long commentid, @GenericTable String commentType) {
		String sql = genSql(updateCommentAgreeSql, commentType);
		jdbcTemplate.update(sql, commentid);
	}

	@CacheDelete(type = CacheCategoryType.COMMENT)
	public void updateCommentDisagree(@Fk Object resourceId, Long commentId, @GenericTable String commentType) {
		String sql = genSql(updateCommentDisagreeSql, commentType);
		jdbcTemplate.update(sql, commentId);
	}

	//TODO:目前存在一个bug，将一条评论删了后，不会将cache清除
	@CacheDelete(type = CacheCategoryType.COMMENT)
	public void deleteComment(@Fk Object resourceId, Long commentId, @GenericTable String commentType, Long actionerId) {
		String sql = genSql(deleteCommentSql, commentType);
		jdbcTemplate.update(sql, commentId, actionerId, actionerId);
	}

	@Cache(keyPattern = "comment-:id", type = CacheCategoryType.COMMENT)
	public Comment queryCommentById(@Param("id") Long commentId, @GenericTable String commentType) {
		String sql = genSql(queryCommentByIdSql, commentType);
		List<Comment> comments = jdbcTemplate.query(sql, CommentMapper.getMapper(commentType), commentId);
		if (CollectionUtils.isEmpty(comments)) {
			return null;
		} else {
			return comments.get(0);
		}

	}

}
